<?php
require_once('./define/define_table.php');
require_once('./class/Adherent.php');
require_once('./class/Association.php');
require_once('./class/Individu.php');
class Adhesion{

	private $id_individu;
	private $id_association;
	private $login;
	private $password;
	private $cout_cotisation;
	private $date_inscription;

	public function Adhesion(){
	}

	public function getIdIndividu(){
		return $this->id_individu;
	}

	public function getIdAssociation(){
		return $this->id_association;
	}

	public function getLogin(){
		return $this->login;
	}
	public function getPassword(){
		return $this->password;
	}
	public function getCoutCotisation(){
		return $this->cout_cotisation;
	}
	public function getDateInscription(){
		return $this->date_inscription;
	}

	public function setIdIndividu($id){
		$this->id_individu = $id;
	}

	public function setIdAssociation($id){
		$this->id_association = $id;
	}

	public function setLogin($login){
		$this->login = $login;
	}

	public function setPassword($pass){
		$this->password = md5($pass);
	}

	public function setCoutCotisation($cout){
		$this->cout_cotisation = $cout;
	}

	public function setDateInscription($date){
		$this->date_inscription = date('Y-m-d', strtotime($date));
	}


	public function getAllAdhesion($id_association = null){
		global $db;
		try{
			if($id_association == null){
				$sql = "SELECT id_individu, id_association, login, password, cout_cotisation, date_inscription	FROM ".ADHESION_TABLE;
				$sth = $db->prepare($sql);
			}
			else{
				$sql = "SELECT id_individu, id_association, login, password, cout_cotisation, date_inscription	FROM ".ADHESION_TABLE."
		        		WHERE id_association = :id_association";
				$sth = $db->prepare($sql);
				$sth->bindValue(":id_association", $id_association, PDO::PARAM_INT);
			}
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "Adhesion");
		}catch(PDOException $e){
			echo $e->getMessage();
		}
	}
	
	public function getAllAdhesionNotPayed($id_association = null){
	    global $db;
	    try{
	        if($id_association == null){
	            $sql = "SELECT id_individu, id_association, login, password, cout_cotisation, date_inscription	FROM ".ADHESION_TABLE."
	            		WHERE cout_cotisation = -1";
	            $sth = $db->prepare($sql);
	        }
	        else{
	            $sql = "SELECT id_individu, id_association, login, password, cout_cotisation, date_inscription	FROM ".ADHESION_TABLE."
	    		        		WHERE id_association = :id_association
	            				AND cout_cotisation = -1";
	            $sth = $db->prepare($sql);
	            $sth->bindValue(":id_association", $id_association, PDO::PARAM_INT);
	        }
	        $sth->execute();
	        PrintQuery::queryPrint($sth);
	        return $sth->fetchAll(PDO::FETCH_CLASS, "Adhesion");
	    }catch(PDOException $e){
	        echo $e->getMessage();
	    }
	}
	
	
	public function getAllAdhesionWithDate($id_association = null, $start_date = "", $end_date = ""){
	    global $db;
	    try{
	        $start_date = ($start_date == "") ? '0000-00-00' : $start_date;
	        $end_date = ($end_date == "") ? date('Y-m-d', time()) : $end_date;
	      
	        
	        if($id_association == null){
	            $sql = "SELECT id_individu, id_association, login, password, cout_cotisation, date_inscription	FROM ".ADHESION_TABLE." 
	            		WHERE 
	            			date_inscription >= :start_date 
	            			AND date_inscription <= :end_date ";
	            $sth = $db->prepare($sql);
	            $sth->bindValue(":start_date", $start_date);
	            $sth->bindValue(":end_date", $end_date);
	        }
	        else{
	            $sql = "SELECT id_individu, id_association, login, password, cout_cotisation, date_inscription	FROM ".ADHESION_TABLE."
			        		WHERE 
			        			id_association = :id_association
	            				AND date_inscription >= :start_date 
	            				AND date_inscription <= :end_date ";
	            $sth = $db->prepare($sql);
	            $sth->bindValue(":id_association", $id_association, PDO::PARAM_INT);
	            $sth->bindValue(":start_date", $start_date);
	            $sth->bindValue(":end_date", $end_date);
	        }
	        $sth->execute();
	        PrintQuery::queryPrint($sth);
	        return $sth->fetchAll(PDO::FETCH_CLASS, "Adhesion");
	    }catch(PDOException $e){
	        echo $e->getMessage();
	    }
	}
	
	public function getAllAdhesionWithDateNotPayed($id_association = null, $start_date = "", $end_date = ""){
	 global $db;
	    try{
	        $start_date = ($start_date == "") ? '0000-00-00' : $start_date;
	        $end_date = ($end_date == "") ? date('Y-m-d', time()) : $end_date;
	      
	        
	        if($id_association == null){
	            $sql = "SELECT id_individu, id_association, login, password, cout_cotisation, date_inscription	FROM ".ADHESION_TABLE." 
	            		WHERE 
	            			date_inscription >= :start_date 
	            			AND date_inscription <= :end_date 
	            			AND cout_cotisation = -1";
	            $sth = $db->prepare($sql);
	            $sth->bindValue(":start_date", $start_date);
	            $sth->bindValue(":end_date", $end_date);
	        }
	        else{
	            $sql = "SELECT id_individu, id_association, login, password, cout_cotisation, date_inscription	FROM ".ADHESION_TABLE."
			        		WHERE 
			        			id_association = :id_association
	            				AND date_inscription >= :start_date 
	            				AND date_inscription <= :end_date
	            				AND cout_cotisation = -1 ";
	            $sth = $db->prepare($sql);
	            $sth->bindValue(":id_association", $id_association, PDO::PARAM_INT);
	            $sth->bindValue(":start_date", $start_date);
	            $sth->bindValue(":end_date", $end_date);
	        }
	        $sth->execute();
	        PrintQuery::queryPrint($sth);
	        return $sth->fetchAll(PDO::FETCH_CLASS, "Adhesion");
	    }catch(PDOException $e){
	        echo $e->getMessage();
	    }
	}

	public function getAdhesion($id_individu, $id_association){
		global $db;
		try{
			$sql = "SELECT id_individu, id_association, login, password, cout_cotisation, date_inscription	FROM ".ADHESION_TABLE."
			        		WHERE id_association = :id_association AND id_individu = :id_individu";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_association", $id_association, PDO::PARAM_INT);
			$sth->bindValue(":id_individu", $id_individu, PDO::PARAM_INT);
			$sth->execute();
			return $sth->fetchObject("Adhesion");
		}catch(PDOException $e){
			echo $e->getMessage();
		}
	}

	public function addOrModify(){
		global $db;
		try{
			$sql = "SELECT COUNT(*) FROM ".ADHESION_TABLE." WHERE id_individu = :id_individu AND id_association = :id_association";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
			$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
			$sth->execute();
			$nb = $sth->fetch();
			if($nb[0] == 0){
				// Ajout
				$sql = "INSERT INTO ".ADHESION_TABLE."
	            		VALUES(	:id_individu, :id_association, :login, :password, 
	            				:cout_cotisation, :date_inscription)";
				$sth = $db->prepare($sql);
				$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
				$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
				$sth->bindValue(":login", $this->login, PDO::PARAM_STR);
				$sth->bindValue(":password", $this->password, PDO::PARAM_STR);
				$sth->bindValue(":cout_cotisation", $this->cout_cotisation);
				$sth->bindValue(":date_inscription", $this->date_inscription);
			}
			else{ // Update
				$sql = "UPDATE ".ADHESION_TABLE."
	            	    SET login = :login,
	            	    	password =  :password, 
	            	        cout_cotisation = :cout_cotisation,
	            	        date_inscription = :date_inscription
	            	    WHERE id_individu = :id_individu
	            	    AND
	            	    id_association =  :id_association";
				$sth = $db->prepare($sql);
				$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
				$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
				$sth->bindValue(":login", $this->login, PDO::PARAM_STR);
				$sth->bindValue(":password", $this->password, PDO::PARAM_STR);
				$sth->bindValue(":cout_cotisation", $this->cout_cotisation);
				$sth->bindValue(":date_inscription", $this->date_inscription);
			}
			$sth->execute();
			PrintQuery::queryPrint($sth);
		}catch(PDOException $e){
			echo $e->getMessage();
		}
	}

	public function delete(){
		global $db;
		try{
			$sql = "DELETE FROM ".ADHESION_TABLE." WHERE id_individu = :id_individu AND id_association = :id_association";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
			$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
			$sth->execute();
		}catch(PDOException $e){
			echo $e->getMessage();
		}
	}

	public function getAddFormIndividu(){
		$html = '<form action="?module=adhesion&action=addContinue" method="post">
	    		<table border="1">
	            	<tr>
	            		<td>'._INDIVIDU_.'</td>
	            		<td>'.Individu::generateSelect().'</td>
	            	</tr>
	            	<tr>
	            		<td colspan="2"><input type="submit" value="'._CONTINUE_.'" /></td>
	            	</tr>
	            </table></form>';
		return $html;
	}

	public function getAddFormNonAdherent($id_individu){
		$html = '<form action="?module=adhesion&action=doAddNonAdherent" method="post">
	    		<table border="1">
	            	<tr>
	            		<td>'._INDIVIDU_.'</td>
	            		<td>'.Individu::generateSelect($id_individu).'</td>
	            	</tr>
	            	<tr>
	            		<td>'._ASSOCIATION_.'</td>
	            		<td>'.Association::generateSelect().'</td>
	            	</tr>
	            	<tr>
	            		<td>'._EMAIL_ADHERENT_.'</td>
	            		<td><input type="text" name="email" /></td>
	            	</tr>
	            	<tr>
	            		<td>'._FILIERE_ADHERENT_.'</td>
	            		<td><input type="text" name="filiere" /></td>
	            	</tr>
	            	<tr>
	            		<td>'._PROMOTION_ADHERENT_.'</td>
	            		<td><input type="text" name="promotion" /></td>
	            	</tr>
	            	<tr>
	            		<td>'._LOGIN_.'</td>
	            		<td><input type="text" name="login" /></td>
	            	</tr>
	            	<tr>
	            		<td>'._PASSWORD_.'</td>
	            		<td><input type="text" name="password" /></td>
	            	</tr>
	            	<tr>
	            		<td>'._COUT_COTISATION_.'</td>
	            		<td><input type="text" name="cout_cotisation" /></td>
	            	</tr>
	            	<tr>
	            		<td>'._DATE_INSCRIPTION_.'</td>
	            		<td><input type="text" name="date_inscription" class="datepicker" /></td>
	            	</tr>
	            	<tr>
	            		<td colspan="2"><input type="submit" value="'._ADD_.'" /></td>
	            	</tr>
	            </table>';
		return $html;
	}

	public function getAddFormAdherent($id_individu){
		$html = '<form action="?module=adhesion&action=doAddAdherent" method="post">
	    	    		<table border="1">
	    	            	<tr>
	    	            		<td>'._INDIVIDU_.'</td>
	    	            		<td>'.Individu::generateSelect($id_individu).'</td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td>'._ASSOCIATION_.'</td>
	    	            		<td>'.Association::generateSelect().'</td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td>'._LOGIN_.'</td>
	    	            		<td><input type="text" name="login" /></td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td>'._PASSWORD_.'</td>
	    	            		<td><input type="text" name="password" /></td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td>'._COUT_COTISATION_.'</td>
	    	            		<td><input type="text" name="cout_cotisation" /></td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td>'._DATE_INSCRIPTION_.'</td>
	    	            		<td><input type="text" name="date_inscription" class="datepicker" /></td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td colspan="2"><input type="submit" value="'._ADD_.'" /></td>
	    	            	</tr>
	    	            </table>';
		return $html;
	}
	
	public function getModifyForm(){
	        $html = '<form action="?module=adhesion&action=doModify" method="post">
	    	    		<table border="1">
	    	            	<tr>
	    	            		<td>'._INDIVIDU_.'</td>
	    	            		<td><input type="text" value="'.$this->id_individu.'" readonly name="id_individu" /></td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td>'._ASSOCIATION_.'</td>
	    	            		<td><input type="text" value="'.$this->id_association.'" readonly name="id_association" /></td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td>'._LOGIN_.'</td>
	    	            		<td><input type="text" name="login" value="'.$this->login.'" /></td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td>'._PASSWORD_.'</td>
	    	            		<td><input type="text" name="password" value="'.$this->password.'" /></td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td>'._COUT_COTISATION_.'</td>
	    	            		<td><input type="text" name="cout_cotisation" value="'.$this->cout_cotisation.'" /></td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td>'._DATE_INSCRIPTION_.'</td>
	    	            		<td><input type="text" name="date_inscription" value="'.$this->date_inscription.'" class="datepicker" /></td>
	    	            	</tr>
	    	            	<tr>
	    	            		<td colspan="2"><input type="submit" value="'._EDIT_.'" /></td>
	    	            	</tr>
	    	            </table>';
	        return $html;
	}
	    
}
?>
